/**
 * Created by Jack Xue on 20101124
 */
package org.code.jack.jweberp.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.code.jack.jweberp.domain.Employee;
import org.code.jack.jweberp.service.IEmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

@Controller("/employee")
public class EmployeeAction extends BaseAction {

	@Resource
	private IEmployeeService employeeServiceBean;

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return this.list(mapping, form, request, response);
	}

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<Employee> employeeList = employeeServiceBean.getEntityList();
		request.setAttribute("result", employeeList);
		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return this.edit(mapping, form, request, response);
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		if(StringUtils.hasLength(id)){
			Employee employee = employeeServiceBean.getEntity(Long.parseLong(id));
			request.setAttribute("obj", employee);
		}
		return mapping.findForward("edit");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Employee employee = (Employee) webForm.form2Obj(Employee.class, true);
		employeeServiceBean.createEntity(employee);
		request.setAttribute("msg", employee.getId() != null ? "添加成功" : "添加失败");
		request.setAttribute("newurl", veerUrl);
		return mapping.findForward("list");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		if(StringUtils.hasLength(id)){
			Employee employee = employeeServiceBean.getEntity(Long.parseLong(id));
			webForm.form2Obj(employee, true);
			boolean status = employeeServiceBean.updateEntity(employee);
			request.setAttribute("msg", status ? "更新成功":"更新失败");
			request.setAttribute("newurl", veerUrl);
		}
		return mapping.findForward("list");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		if(StringUtils.hasLength(id)){
			boolean status = employeeServiceBean.deleteEntity(Long.parseLong(id));
			request.setAttribute("msg", status ? "删除成功":"删除失败");
			request.setAttribute("newurl", veerUrl);
		}
		return mapping.findForward("list");
	}
}
